package cn.tedu.controller;

import cn.tedu.pojo.User;
import cn.tedu.service.UserService;
import cn.tedu.vo.PageResult;
import cn.tedu.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    //注册用户
    @PostMapping("/register/addUser")
    public SysResult addUser(@RequestBody User user){
        user=userService.addUser(user);
        if(user!=null ){
            return SysResult.fail();
        }
        return SysResult.success();
    }
    //用户登录
    @PostMapping("/user/userModel")
    public SysResult login(@RequestBody User user){
        String token=userService.login(user);
        if (token==null||token.length()==0){
            return SysResult.fail();
        }
        return SysResult.success(token);
    }
    //查询手机号是否注册
    @PostMapping("/forgetPassword/queryPhone")
    public SysResult queryPhone(@RequestBody User user){
        user=userService.queryPhone(user);
        if(user==null){
            return SysResult.fail();
        }
        return SysResult.success();
    }
    //重置密码
    @PutMapping("/forgetPasswoed/resetPassword")
    public SysResult resetPassword(@RequestBody User user){
        userService.resetPassword(user);
        return SysResult.success();
    }
    //查询用户列表
    @GetMapping("/user/find")
    public SysResult findUserAll(PageResult pageResult){
        pageResult=userService.findUserAll(pageResult);
        return SysResult.success(pageResult);
    }
    //删除用户
    @DeleteMapping("/user/{id}")
    public SysResult deleteUser(@PathVariable Integer id){
        userService.deleteUser(id);
        return SysResult.success();
    }
    //添加用户
    @PostMapping("/user/addUser")
    public SysResult addUser2(@RequestBody User user){
        userService.addUser2(user);
       return SysResult.success();
    }
    //回显用户
    @GetMapping("/user/{id}")
    public SysResult selectUser(@PathVariable Integer id){
        User user=userService.selectUser(id);
        return SysResult.success(user);
    }
    //修改用户
    @PutMapping("/user/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }
}
